package com.spc.car.data.service.project.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.spc.car.data.constant.CommConstants;
import com.spc.car.data.domain.bond.BiddingBondCollectVO;
import com.spc.car.data.domain.bond.BiddingBondReturnVO;
import com.spc.car.data.domain.project.dto.BiddingBondCollectDTO;
import com.spc.car.data.domain.project.dto.BiddingBondReturnDTO;
import com.spc.car.data.entity.project.ProjectBiddingBond;
import com.spc.car.data.entity.sys.SysUser;
import com.spc.car.data.mapper.project.ProjectBiddingBondMapper;
import com.spc.car.data.service.bond.BondRecordService;
import com.spc.car.data.service.project.ProjectBiddingBondService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.validation.constraints.NotBlank;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 投标项目保证金 服务实现类
 * </p>
 *
 * @author 
 * @since 2020-04-23
 */
@Service
public class ProjectBiddingBondServiceImpl extends ServiceImpl<ProjectBiddingBondMapper, ProjectBiddingBond> implements ProjectBiddingBondService {

    @Autowired
    BondRecordService bondRecordService;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void biddingBondCollect(BiddingBondCollectVO bondCollectVO, SysUser user) {
        ProjectBiddingBond biddingBond = new ProjectBiddingBond();
        biddingBond.setCollectStatus(CommConstants.BondConstants.BOND_WAIT_COLLECT);
        biddingBond.setOperatorId(user.getUserId());
        biddingBond.setOperator(user.getUserName());
        biddingBond.setCreateTime(new Date());
        BeanUtils.copyProperties(bondCollectVO, biddingBond);
        saveOrUpdate(biddingBond);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void biddingBondReturn(BiddingBondReturnVO bondReturnVO, SysUser user) {

        ProjectBiddingBond biddingBond =
                getOne(new LambdaQueryWrapper<ProjectBiddingBond>()
                        .eq(ProjectBiddingBond::getProjectCode, bondReturnVO.getProjectCode())
                        .eq(ProjectBiddingBond::getSectionCode, bondReturnVO.getSectionCode())
                        .eq(ProjectBiddingBond::getOrgCode, bondReturnVO.getOrgCode()));

        biddingBond.setReturnStatus(CommConstants.BondConstants.BOND_RETURN);
        biddingBond.setOperator(user.getUserName());
        biddingBond.setOperatorId(user.getUserId());
        biddingBond.setCreateTime(new Date());
        BeanUtils.copyProperties(bondReturnVO, biddingBond);
        saveOrUpdate(biddingBond);
    }

    @Override
    public List<BiddingBondCollectDTO> listBiddingBondCollect(@NotBlank String projectCode,
                                                              @NotBlank String sectionCode,
                                                              @NotBlank String status) {
        return baseMapper.listBiddingBondCollect(projectCode, sectionCode, status);
    }

    @Override
    public List<BiddingBondReturnDTO> listBiddingBondReturn(@NotBlank String projectCode,
                                                            @NotBlank String sectionCode,
                                                            @NotBlank String status) {
        return baseMapper.listBiddingBondReturn(projectCode, sectionCode, status);
    }
}
